//
//  BookCitySearchViewController.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/5.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "BookCitySearchViewController.h"
#import "CZQSearchView.h"
#import "CZQCustomNavView.h"
#import "BookCitySearchView.h"

@interface BookCitySearchViewController () <UITextFieldDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate,UITableViewDataSource>


/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@property (nonatomic , weak) CZQSearchView *searchView;

@property(nonatomic,weak)CZQCustomNavView *navView;

@property(nonatomic,weak)UICollectionView *collectionView;

@property(nonatomic,weak)UICollectionViewFlowLayout *collectViewFlowLayout;

@end

@implementation BookCitySearchViewController

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatUI];
    
}

-(void)creatUI
{
    
    
    
    
    
    
    //
    UICollectionViewFlowLayout *collectViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectViewFlowLayout = collectViewFlowLayout;
    //设置cell之间间距
//    collectViewFlowLayout.minimumInteritemSpacing = 30;
    //设置行距
//    collectViewFlowLayout.minimumLineSpacing = 30;
    collectViewFlowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    CGFloat itemWith = (SCREEN_WIDTH - 90) / 3;
    
    collectViewFlowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50);
    
    //设置Item大小
    collectViewFlowLayout.estimatedItemSize = CGSizeMake(itemWith, 30);
    //    collectViewFlowLayout.itemSize = CGSizeMake(itemWith, itemWith + 60);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:collectViewFlowLayout];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = CSClearColor;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:[BookCitySearchCellOne class] forCellWithReuseIdentifier:@"cell1"];
    [collectionView registerClass:[BookCitySearchCellTwo class] forCellWithReuseIdentifier:@"cell2"];
    [collectionView registerClass:[BookCitySearchView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"head"];
    
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    
   
    [collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.left.width.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(adaptNavagationBar);
        
        
    }];
    

    
    CZQCustomNavView *navView = [CZQCustomNavView new];
    self.navView = navView;
    [self.view addSubview:navView];
    [navView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.view);
        make.height.mas_equalTo(adaptNavagationBar);
    }];
    
    navView.popBtn.hidden = YES;
    [navView.rightBtn setTitle:@"取消"];
    navView.rightBtn.hidden = NO;
    [navView.rightBtn addTarget:self action:@selector(rightBtnClick)];
    
    CZQSearchView *searchView = [[CZQSearchView alloc] initWithFrame:CGRectMake(15, AdaptStatusBar + 7, SCREEN_WIDTH - 80, 30)];
    self.searchView = searchView;
    searchView.seachField.placeholder = @"请输入你想看的书名\作者名\主角名";
    searchView.seachField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchView.seachField.placeholder attributes:@{NSForegroundColorAttributeName: CSColorFromRGB(0xDCDCDC)}];
    searchView.backgroundColor = CSWhiteColor;
    searchView.layer.shadowColor = CSGrayColor.CGColor;
    searchView.layer.shadowOffset = CGSizeMake(1,0);
    searchView.layer.shadowOpacity = 0.3;//阴影
    searchView.layer.cornerRadius = 15;
    searchView.seachField.returnKeyType = UIReturnKeySearch;
    searchView.seachField.delegate = self;
    searchView.backgroundColor = CSWhiteColor;
    [searchView.seachField addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    [navView addSubview:searchView];
    
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 10;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        BookCitySearchCellOne *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell.rankLable.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        
        if (indexPath.row == 0) {
            
            cell.rankLable.backgroundColor = CSColorFromRGB(0xF52C50);
            
        }else if (indexPath.row == 1){
            
            cell.rankLable.backgroundColor = CSColorFromRGB(0xFE7F20);
            
        }
        else if (indexPath.row == 2){
            
            cell.rankLable.backgroundColor = CSColorFromRGB(0xF7C41D);
            
        }else
        {
            cell.rankLable.backgroundColor = CSColorFromRGB(0xDCDCDC);
        }
        
        return cell;
        
    }else
    {
        BookCitySearchCellTwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        if (indexPath.row == 2) {
            cell.titleLable.text = @"斗罗大陆斗罗大陆斗罗大陆斗罗大陆斗罗大陆斗罗大陆斗罗大陆";
        }
        return cell;
        
    }
    
    
    
    
}

//设置cell的大小

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 20;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
        
        BookCitySearchView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        
        if (indexPath.section == 0) {
            headView.delBtn.hidden = YES;
            headView.titleLable.text = @"热门搜索";
        }else
        {
            headView.delBtn.hidden = NO;
            headView.titleLable.text = @"最近搜索";
        }
        
        return headView;
        
        
        
    }
    return reusableview;
    
}





#pragma mark UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    
    
    
}

#pragma mark - 名字模糊搜索
-(void)changeText:(UITextField *)field
{
    if (field.text.length == 0) {
//        [self.tableView.mj_header beginRefreshing];
        [self.view endEditing:YES];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField{
    
    //－－－这里就是你要做的操作
    //在这里做你响应return键的代码
//    [self.tableView.mj_header beginRefreshing];
    
    [self.view endEditing:YES];
    
    self.collectionView.hidden = YES;
    
    
    [self.tableView reloadData];
    
    
    
    return YES;
    
}

#pragma mark - 退出
- (void)rightBtnClick
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - tableview


#pragma mark - 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = [UIColor whiteColor];
        
        tableView.estimatedRowHeight = 150;
        tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.left.width.mas_equalTo(self.view);
            make.top.mas_equalTo(self.view).offset(adaptNavagationBar);
            
            
        }];
        
        
        UIButton *headTipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        headTipBtn.frame = CGRectMake(0, 0, SCREENWIDTH, 40);
        [headTipBtn setTitle:@"“对不起，没有相关结果，您可以尝试以下书籍”" forState:UIControlStateNormal];
        [headTipBtn setTitleColor:CSColorFromRGB(0xF52C50) forState:UIControlStateNormal];
        headTipBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        headTipBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        headTipBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
        tableView.tableHeaderView = headTipBtn;
        
        //注册
        [tableView registerClass:[BookCitySearchCell class] forCellReuseIdentifier:@"cell"];
        
        [self.view bringSubviewToFront:self.navView];
        
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count + 10;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BookCitySearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
